﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using SuperInterMarket.Data;

namespace SuperInterMarket.Mail
{
    public partial class FromAddressManage : Form
    {
        private List<FromAddress> froms = new List<FromAddress>();
        public FromAddressManage()
        {
            InitializeComponent();
        }

        private List<XElement> GetXElements()
        {
            XDocument doc = XDocument.Load(SysConstants.FromAddressFile);
            XElement config = doc.Element("config");
            List<XElement> froms = config.Elements("from").ToList<XElement>();
            return froms;
        }
        private List<FromAddress> GetFromAddresses(List<XElement> xes)
        {
            List<FromAddress> allFroms = new List<FromAddress>();

            if (xes.Count > 0)
            {
                foreach (var item in xes)
                {
                    FromAddress from = new FromAddress();
                    from.Email = item.Element("email").Value;
                    from.ServerType = item.Element("servertype").Value;
                    from.UserName = item.Element("username").Value;
                    from.PassWord = item.Element("password").Value;
                    allFroms.Add(from);
                }
            }
            return allFroms;
        }

        public void LoadDataGridView()
        {
            FromAddressDataGridView.DataSource = GetFromAddresses(GetXElements());
           
        }

        private void FromAddressManage_Load(object sender, EventArgs e)
        {
            LoadDataGridView();           
        }
       
        private void AddNewBt_Click(object sender, EventArgs e)
        {
            AddFromAddress from = new AddFromAddress(this);
            from.ShowDialog();
        }

        private void DeleteBt_Click(object sender, EventArgs e)
        {
            if (FromAddressDataGridView.SelectedRows.Count == 0)
            {
                MessageBox.Show("至少选择一个");
                return;
            }

            string email = (string)((FromAddressDataGridView.SelectedRows)[0]).Cells[0].Value;
            string type = (string)((FromAddressDataGridView.SelectedRows)[0]).Cells[1].Value;

            XDocument doc = XDocument.Load(SysConstants.FromAddressFile);
            XElement config = doc.Element("config");
            List<XElement> froms = config.Elements("from").ToList<XElement>();
            if (froms.Count > 0)
            {
                XElement element = froms.Where(c => c.Element("email").Value == email).Where(c => c.Element("servertype").Value == type).SingleOrDefault();
                if (element != null)
                {
                    element.Remove();
                }
            }
            doc.Save(SysConstants.FromAddressFile);
            LoadDataGridView();
        }

      
       
    }
}
